package com.seocoo.worker.order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.seocoo.worker.R;
import com.seocoo.worker.order.entity.RxCancelInstallSuccess;
import com.seocoo.worker.order.model.OrderCancelInstallModel;
import com.seocoo.worker.order.req.ReqCancelInstall;
import com.sfh.lib.event.BusEventManager;
import com.sfh.lib.mvvm.LiveDataMatch;
import com.sfh.lib.ui.MVVMActivity;

/**
 * author : Ssp
 * date   : 2020/2/22 23:54
 * desc   : 取消安装
 */
public class OrderCancelInstallationActivity extends MVVMActivity implements View.OnClickListener {

    private TextView tv_submit;
    private EditText et_reason;
    private ImageView iv_close;
    private String installId;

    public static void start(Context context, String installId) {
        Intent intent = new Intent(context, OrderCancelInstallationActivity.class);
        intent.putExtra("installId", installId);
        context.startActivity(intent);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_cancle_installation_activity);
        installId = getIntent().getStringExtra("installId");
        initView();
        initListener();
    }

    private void initView() {

        tv_submit = findViewById(R.id.tv_submit);
        et_reason = findViewById(R.id.et_reason);
        iv_close = findViewById(R.id.iv_close);
    }

    private void initListener() {
        iv_close.setOnClickListener(this);
        tv_submit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_submit) {
            // 提交
            String reason = et_reason.getText().toString().trim();
            if (TextUtils.isEmpty(reason)) {
                showDialogToast("请输入取消安装原因!");
                return;
            }
            OrderCancelInstallModel viewModel = getViewModel(OrderCancelInstallModel.class);
            ReqCancelInstall reqCancelInstall = new ReqCancelInstall();
            reqCancelInstall.setInstallId(installId);
            reqCancelInstall.setReason(reason);
            viewModel.cancelInstall(reqCancelInstall);
        }
        if (view.getId() == R.id.iv_close) {
            //关闭
            finish();
        }
    }

    @LiveDataMatch
    public void onCancelSuccess() {
        //取消安装成功
        BusEventManager.postEvent(new RxCancelInstallSuccess(true));
        OrderCancelInstallSuccessActivity.start(this);
        finish();
    }
}
